package com.git;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.util.Properties;

@Component
@Getter
public class VersionInfo {

    private String version = "unknown";
    private String changelog = "Changelog not available";

    @Autowired
    private ResourceLoader resourceLoader;

    @PostConstruct
    public void init() {
        try {
            loadVersionFromPackageJson();
            loadChangelog();
        } catch (Exception e) {
            // 使用默认值，不抛出异常避免应用启动失败
            System.err.println("Failed to load version info: " + e.getMessage());
        }
    }

    private void loadVersionFromPackageJson() {
        try {
            // 尝试从 classpath 读取
            Resource resource = resourceLoader.getResource("classpath:package.json");
            if (resource.exists()) {
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode jsonNode = objectMapper.readTree(resource.getInputStream());
                this.version = jsonNode.get("version").asText("unknown");
                return;
            }

            // 尝试从文件系统读取（开发环境）
            Resource fileResource = resourceLoader.getResource("file:package.json");
            if (fileResource.exists()) {
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode jsonNode = objectMapper.readTree(fileResource.getInputStream());
                this.version = jsonNode.get("version").asText("unknown");
                return;
            }

            // 从 Maven properties 读取备选方案
            try {
                Properties properties = new Properties();
                properties.load(getClass().getClassLoader().getResourceAsStream("META-INF/maven/your-groupId/your-artifactId/pom.properties"));
                this.version = properties.getProperty("version", "unknown");
            } catch (Exception e) {
                // 忽略异常
            }

        } catch (Exception e) {
            System.err.println("Error loading package.json: " + e.getMessage());
        }
    }

    private void loadChangelog() {
        try {
            // 尝试从 classpath 读取
            Resource resource = resourceLoader.getResource("classpath:CHANGELOG.md");
            if (resource.exists()) {
                this.changelog = new String(resource.getInputStream().readAllBytes());
                return;
            }

            // 尝试从文件系统读取（开发环境）
            Resource fileResource = resourceLoader.getResource("file:CHANGELOG.md");
            if (fileResource.exists()) {
                this.changelog = new String(fileResource.getInputStream().readAllBytes());
                return;
            }

        } catch (Exception e) {
            System.err.println("Error loading CHANGELOG.md: " + e.getMessage());
        }
    }
}
